/**
 * 
 */
package jnzb.control.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jnzb.control.Configuration;

/**
 * File based implementation of the logger
 * 
 * @author Rid
 * 
 */
public class SimpleFileLogger implements Logger {

	private BufferedWriter logFile = null;
	private static SimpleFileLogger INSTANCE=null;

	public static Logger getInstace() {
		if (INSTANCE==null){
			INSTANCE = new SimpleFileLogger();
		}
		return INSTANCE;
	}

	private SimpleFileLogger() {
		String logfilename = Configuration.getInstance().getLogfilename();
		if (logfilename != null) {
			try {
				// Append to existing logfile
				logFile = new BufferedWriter(new FileWriter(new File(
						logfilename), true));
			} catch (IOException e) {
				System.err.println(e);
				logFile = null;
			}
		}
	}

	/**
	 * Writes a string to the log file
	 * 
	 * @see jnzb.control.util.Logger#writeRaw(java.lang.String)
	 */
	@Override
	public void writeRaw(String msg) {
		if (logFile != null) {
			try {
				logFile.newLine();
				logFile.flush();
				logFile.write(msg);
			} catch (IOException e) {
				// Cant log
				System.err.println(e);
			}
		}

	}

	@Override
	protected void finalize() throws Throwable {
		if (logFile != null) {
			logFile.flush();
			logFile.close();
		}
	}

}
